package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.AnnualWorkDays;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.IndicatorSixMapper;
import com.ruoyi.business.domain.IndicatorSix;
import com.ruoyi.business.service.IIndicatorSixService;

import javax.validation.Validator;

/**
 * 指标六Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-19
 */
@Service
public class IndicatorSixServiceImpl   extends BaseServiceImpl implements IIndicatorSixService
{
    @Autowired
    private IndicatorSixMapper indicatorSixMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询指标六
     * 
     * @param id 指标六主键
     * @return 指标六
     */
    @Override
    public IndicatorSix selectIndicatorSixById(Long id)
    {
        return indicatorSixMapper.selectIndicatorSixById(id);
    }

    /**
     * 查询指标六列表
     * 
     * @param indicatorSix 指标六
     * @return 指标六
     */
    @Override
    public List<IndicatorSix> selectIndicatorSixList(IndicatorSix indicatorSix)
    {
        return indicatorSixMapper.selectIndicatorSixList(indicatorSix);
    }

    /**
     * 新增指标六
     * 
     * @param indicatorSix 指标六
     * @return 结果
     */
    @Override
    public int insertIndicatorSix(IndicatorSix indicatorSix)
    {
        indicatorSix.setCreateTime(DateUtils.getNowDate());
        return indicatorSixMapper.insertIndicatorSix(indicatorSix);
    }

    /**
     * 修改指标六
     * 
     * @param indicatorSix 指标六
     * @return 结果
     */
    @Override
    public int updateIndicatorSix(IndicatorSix indicatorSix)
    {
        indicatorSix.setUpdateTime(DateUtils.getNowDate());
        return indicatorSixMapper.updateIndicatorSix(indicatorSix);
    }

    /**
     * 批量删除指标六
     * 
     * @param ids 需要删除的指标六主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorSixByIds(Long[] ids)
    {
        return indicatorSixMapper.deleteIndicatorSixByIds(ids);
    }

    /**
     * 删除指标六信息
     * 
     * @param id 指标六主键
     * @return 结果
     */
    @Override
    public int deleteIndicatorSixById(Long id)
    {
        return indicatorSixMapper.deleteIndicatorSixById(id);
    }

    @Override
    public String importData(List<IndicatorSix> userList, boolean updateSupport, String operName) {
        if (userList == null || userList.isEmpty()) {
            throw new ServiceException("导入指标数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();

        int i = 1;
        for (IndicatorSix user : userList) {
            try {
                i++;
                if (user.getAwardYear() == null || user.getAwardYear().equals("") || user.getEmployeeId() ==null || user.getEmployeeId().equals("") || !isAwardYearValid(user.getAwardYear())) {
                    failureNums.add(String.valueOf(i));
                    failureNum++;
                    continue ;
                }
                // 验证是否存在这个年度工作日
                IndicatorSix annualWorkDays = new IndicatorSix();
                annualWorkDays.setEmployeeId(user.getEmployeeId());
                annualWorkDays.setAwardYear(user.getAwardYear());

                List<IndicatorSix> selectAnnualWorkDaysList = indicatorSixMapper.selectIndicatorSixList(annualWorkDays);

                if (selectAnnualWorkDaysList.isEmpty()) {
                    // 验证数据
                    BeanValidators.validateWithException(validator, user);
                    // 设置初始值
                    user.setCreateTime(DateUtils.getNowDate());
                    user.setCreateBy(operName);
                    indicatorSixMapper.insertIndicatorSix(user);
                    successNum++;
                    //successMsg.append("<br/>" + successNum + "、年度工作日 "  + " 导入成功");
                } else if (true) {
                    // 验证数据
                    IndicatorSix existingWorkDay = selectAnnualWorkDaysList.get(0);
                    BeanValidators.validateWithException(validator, user);
                    user.setId(existingWorkDay.getId());
                    user.setUpdateTime(DateUtils.getNowDate());
                    user.setUpdateBy(operName);
                    indicatorSixMapper.updateIndicatorSix(user);
                    successNum++;
                    //successMsg.append("<br/>" + successNum + "、年度工作日 "  + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、指标 " +  " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、指标 " + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
            //throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        setCacheInfo("指标",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

        return successMsg.toString() + failureMsg.toString();
    }
}
